package com.cuijq.dubbo.core.registey.impl;

import com.cuijq.dubbo.core.registey.Registry;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

// 2. 基于内存的注册中心实现
public class SimpleRegistry implements Registry {
    private static final Map<String, List<String>> registry = new ConcurrentHashMap<>();

    @Override
    public void register(String serviceName, String serviceAddress) {
        registry.computeIfAbsent(serviceName, k -> new CopyOnWriteArrayList<>()).add(serviceAddress);
        System.out.println("Registered service: " + serviceName + " -> " + serviceAddress);
    }

    @Override
    public List<String> discover(String serviceName) {
        return registry.getOrDefault(serviceName, Collections.emptyList());
    }
}
